package vn.vng.research.fsd.client;

import java.util.LinkedList;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import vn.vng.research.fsd.Configuration;
import vn.vng.research.fsd.services.thrift.IndexService;
import vn.vng.research.fsd.services.thrift.SearchService;
import vn.vng.research.fsd.services.thrift.TResult;
import vn.vng.research.fsd.services.thrift.TSchool;
import vn.vng.research.fsd.services.thrift.TUser;

public class SearchClient {

	/**
	 * @param args
	 */

	public static final int INDEX = 1;
	public static final int SEARCH = 2;
	public static final int FACET = 3;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String usage = "Usage: \"-index\" OR \"-search <queries> [hitsPerPage(=50)] [pageNum(=1)]\" OR \"-facet 0 <schoolid> <queries>  [hitsPerPage(=50)] [pageNum(=1)]\"";
		if (args.length > 0
				&& ("-h".equals(args[0]) || "-help".equals(args[0]))) {
			System.out.println(usage);
			System.exit(0);
		}

		int action = 0;
		String queries = null;
		long searchid = -1;
		int schoolid = -1;
		int hitsPerPage = 50;
		int pageNum = 1;

		if ((args.length > 0) && ("-index".equals(args[0]))
				&& (args.length == 1)) {
			action = INDEX;
		} else if ((args.length > 0) && ("-search".equals(args[0]))
				&& (args.length >= 2)) {
			action = SEARCH;
			queries = args[1];
			if (args.length >= 3){
				hitsPerPage = Integer.parseInt(args[2]);
				if (args.length == 4){
					pageNum = Integer.parseInt(args[3]);
				}
			}
		} else if ((args.length > 0) && ("-facet".equals(args[0]))
				&& (args.length >= 4)) {
			action = FACET;
			searchid = Long.parseLong(args[1]);
			schoolid = Integer.parseInt(args[2]);
			queries = args[3];
			if (args.length >= 5){
				hitsPerPage = Integer.parseInt(args[4]);
				if (args.length == 6){
					pageNum = Integer.parseInt(args[5]);
				}
			}
		} else {
			System.out.println(usage);
			System.exit(0);
		}

		Configuration.load();

		try {
			TTransport transport;

			long start = System.currentTimeMillis();

			if (action == INDEX) {
				System.out.println("Indexing...");

				transport = new TSocket(Configuration.loadHost("index"),
						Configuration.loadPort("index"));
				transport.open();
				TProtocol protocol = new TBinaryProtocol(transport);

				IndexService.Client client = new IndexService.Client(protocol);
				client.fullReindex();

				transport.close();
			} else if (action == SEARCH) {
				System.out.println("Searching...");

				transport = new TSocket(Configuration.loadHost("search"),
						Configuration.loadPort("search"));
				transport.open();
				TProtocol protocol = new TBinaryProtocol(transport);

				SearchService.Client client = new SearchService.Client(protocol);
				TResult result = client.search(0, queries,hitsPerPage,pageNum);

				printResult(result, 20, queries, null);
				transport.close();
			} else if (action == FACET) {
				System.out.println("Faceting...");

				transport = new TSocket(Configuration.loadHost("search"),
						Configuration.loadPort("search"));
				transport.open();
				TProtocol protocol = new TBinaryProtocol(transport);

				SearchService.Client client = new SearchService.Client(protocol);
				LinkedList<Integer> schoolLs = new LinkedList<Integer>();
				schoolLs.add(schoolid);
				TResult result = client.facet(0, searchid, queries, schoolLs,hitsPerPage,pageNum);
				printResult(result, 20, queries, schoolLs);

				transport.close();
			}
			long end = System.currentTimeMillis();
			System.out.println("Total excuting time: " + (end - start) + "ms.");

		} catch (TException x) {
			x.printStackTrace();
		}
	}

	public static void printResult(TResult result, int schoolN,
			String queries, LinkedList<Integer> schoolIDLs) {

		if (result.getSearchID() == -1) {
			System.out.print("There is no profile match this query \""
					+ queries + "\"");
			if (schoolIDLs != null) {
				System.out.print(" study at ");
				for (int i = 0; i < schoolIDLs.size(); i++) {
					System.out.print(schoolIDLs.get(i));
					System.out.print(" ");
				}
			}
			System.out.println();
			return;
		}

		System.out.println("Search ID: " + result.getSearchID());
		System.out.println("Showing "
				+ Math.min(result.getSchoolLs().size(), schoolN)
				+ " school(s) of " + result.getSchoolLs().size());
		for (int i = 0; i < Math.min(result.getSchoolLs().size(), schoolN); i++) {
			TSchool school = result.getSchoolLs().get(i);
			System.out.println("[" + school.getId() + "] "
					+ school.getSchoolname() + " (" + school.getCount() + ")");
		}

		System.out.println("Showing "
				+ result.getUserLs().size() + " user(s) of " + result.getTotalHits());
		for (int i = 0; i < result.getUserLs().size(); i++) {
			TUser user = result.getUserLs().get(i);
			System.out.println(user.getUid() + "|" + user.getUsername() + "|"
					+ user.getEmail() + "|" + user.getDisplayname() + "|"
					+ user.getEducation());
		}

	}

}
